package com.orange.api.util;

import com.orange.api.constant.ConstantValue;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.nio.charset.StandardCharsets;
import java.util.Date;

public class JwtUtil {
    public static String key = "userId";
    // 把String类型变成JWT
    public static String StringToJwt(String value){
        JwtBuilder jwtBuilder = Jwts.builder().claim(key, value)
                .signWith(SignatureAlgorithm.HS256, ConstantValue.single.getBytes(StandardCharsets.UTF_8))
                .setExpiration(new Date(System.currentTimeMillis() + ConstantValue.JWT_TTL));
        return jwtBuilder.compact();
    }

    // 解析JWT
    public static String parseJwt(String token){
        return Jwts.parser().setSigningKey(ConstantValue.single.getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token).getBody().get(key, String.class);
    }
}
